#include <iostream>
#include <queue>
#define INF 0x3f3f3f3f
#define MAXN 105
using namespace std;
int cost[MAXN][MAXN];
int mincost[MAXN];
bool used[MAXN];
int N;
typedef pair<int, int> road;
typedef long long ll;
void solve();
int main() {
    cin >> N;
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
            cin >> cost[i][j];
    solve();
}


// void solve() {
//     fill(mincost, mincost + N, INF);
//     priority_queue<road, vector<road>, greater<road> > q;
//     mincost[0] = 0;
//     q.push(make_pair(0, 0));
//     ll res = 0;
//     while (!q.empty()) {
//         road r = q.top(); q.pop();
//         if (used[r.second] || r.first > mincost[r.second]) continue;
//         int c = r.first, n = r.second; used[n] = true;
//         res += c; 
//         for (int i = 0; i < N; i++) {
//             if (mincost[i] > cost[n][i]) {
//                 mincost[i] = cost[n][i];
//                 q.push(make_pair(mincost[i], i));
//            }
//         }
//     }
//     cout << res << endl;
// }


void solve() {
    fill(mincost, mincost + N, INF);
    mincost[0] = 0;
    ll res = 0;
    while (true) {
        int v = -1;
        for (int i = 0; i < N; i++) {
            if (!used[i] && (v == -1 || mincost[i] < mincost[v])) v = i;
        }
        if (v == -1) break;
        used[v] = true; res += mincost[v];
        for (int i = 0; i < N; i++) {
            mincost[i] = min(mincost[i], mincost[v]);
        }
    }
    cout << res << endl;
}

